package com.cxyd.modules.wms.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cxyd.common.exception.RootException;
import com.cxyd.common.page.PageData;
import com.cxyd.common.service.impl.CrudServiceImpl;
import com.cxyd.common.utils.ConvertUtils;
import com.cxyd.common.utils.DateUtils;
import com.cxyd.common.utils.ProcResult;
import com.cxyd.common.utils.Result;
import com.cxyd.modules.api.report.InoutCount;
import com.cxyd.modules.api.report.OrderCount;
import com.cxyd.modules.security.user.SecurityUser;
import com.cxyd.modules.sys.dto.SysUserDTO;
import com.cxyd.modules.sys.service.SysUserService;
import com.cxyd.modules.wms.dao.OrderDetailDao;
import com.cxyd.modules.wms.dao.OrderReelDao;
import com.cxyd.modules.wms.dto.AllotTempDTO;
import com.cxyd.modules.wms.dto.OrderDetailDTO;
import com.cxyd.modules.wms.dto.OrderReelDTO;
import com.cxyd.modules.wms.dto.StockDTO;
import com.cxyd.modules.wms.entity.*;
import com.cxyd.modules.wms.service.AllotTempService;
import com.cxyd.modules.wms.service.OrderDetailService;
import com.cxyd.modules.wms.service.OrderReelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.*;

/**
 * 出库明细reelid
 *
 * @author lp
 * @since 1.0.0 2024-9-14
 */
@Slf4j
@Service
public class OrderReelServiceImpl extends CrudServiceImpl<OrderReelDao, OrderReelEntity, OrderReelDTO> implements OrderReelService {
    @Resource
    private SysUserService userService;

    @Override
    public PageData<OrderReelDTO> page(Map<String, Object> params) {
        //1.查询主单信息
        final IPage<OrderReelEntity> page = baseDao.selectPage(
                getPage(params, "update_date", false),
                getWrapper(params)
        );
        //2.获取用户信息
        final List<SysUserDTO> userList = userService.getList();
        //3.将creator与updater的username通过id进行绑定
        page.getRecords().forEach(entity -> {
            if (entity.getCreator() != null) {
                entity.setCreatorName(userService.getUsernameById(entity.getCreator(), userList));
            }
            if (entity.getUpdater() != null) {
                entity.setUpdaterName(userService.getUsernameById(entity.getUpdater(), userList));
            }
        });

        return getPageData(page, OrderReelDTO.class);

    }

    @Override
    public QueryWrapper<OrderReelEntity> getWrapper(Map<String, Object> params) {
        final String spuNo = (String) params.get("spuNo");
        final String reelId = (String) params.get("reelId");
        final String contNo = (String) params.get("contNo");
        final String supplier = (String) params.get("supplier");
        final String zllbh = (String) params.get("zllbh");
        final String stockType = (String) params.get("stockType");
        Integer detailSt = ObjectUtils.isNotEmpty(params.get("detailSt"))?Integer.parseInt((String) params.get("detailSt")):null;
        Date startDate = DateUtils.stringToDate((String) params.get("startDate"), DateUtils.DATE_TIME_PATTERN);
        Date endDate = DateUtils.stringToDate((String) params.get("endDate"), DateUtils.DATE_TIME_PATTERN);

        QueryWrapper<OrderReelEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(ObjectUtils.isNotEmpty(detailSt), "detail_st", detailSt);
        wrapper.eq(ObjectUtils.isNotEmpty(stockType), "stock_type", stockType);
        wrapper.like(StringUtils.isNotBlank(spuNo), "spu_no", spuNo);
        wrapper.like(StringUtils.isNotBlank(reelId), "reel_id", reelId);
        wrapper.like(StringUtils.isNotBlank(contNo), "cont_no", contNo);
        wrapper.like(StringUtils.isNotBlank(supplier), "supplier", supplier);
        wrapper.like(StringUtils.isNotBlank(zllbh), "zllbh", zllbh);
        wrapper.ge(ObjectUtils.isNotEmpty(startDate), "update_date", startDate);
        wrapper.le(ObjectUtils.isNotEmpty(endDate), "update_date", endDate);
        return wrapper;
    }

    @Override
    public List<OrderReelDTO> getList(Map<String, Object> params) {
        //1.查询库存信息
        final List<OrderReelEntity> page = baseDao.selectList(
                getWrapper(params).orderByDesc("update_date")
        );
        //2.获取用户列表
        final List<SysUserDTO> userList = userService.getList();
        //3.将creator与updater的username通过id进行绑定
        page.forEach(entity -> {
            if (entity.getCreator() != null) {
                entity.setCreatorName(userService.getUsernameById(entity.getCreator(), userList));
            }
            if (entity.getUpdater() != null) {
                entity.setUpdaterName(userService.getUsernameById(entity.getUpdater(), userList));
            }
        });
        return ConvertUtils.sourceToTarget(page, OrderReelDTO.class);
    }

    @Override
    public List<InoutCount> getOutcount() {

        return baseDao.selectOutNum();
    }

    @Override
    public List<OrderCount> getOrderCount() {

        return baseDao.selectOrderCount();
    }
}